Except if you are inquiring about the actions of a person or perhaps also an animal.
The word why has built in the notion that what you are asking about was a manifestation of someone/something seeking an objective. Why did (s)he do that suggests they were trying to achieve something or get somewhere. It invites an answer which includes an account of what it was they were trying to achieve. This can be extended a bit to animals - "Why did the chicken cross the road" and all that.
Beyond that "Why" is meaningless. "Why does the sun shine" has no answer. "How does the sun shine" is fine - a sensible answer can describe what we know of the mechanism but the sun has no plan or objective against which we can ask "why?".
If a child asks you why this or that is so be careful in answering not to include "so it can ..." or similar response. Do not encourage a child in the notion that items other than persons or some animals can have a plan or objective. A huge amount of confusion and misunderstanding in the world is driven by this dangerous response to a simple little overworked word -"Why". Give it a rest!