DEAR MISS MANNERS: It's long been explained that hats were worn as protection from the sun in times when people spent a great more time outdoors. This makes sense.
And there is ample evidence of this, as in the discontinuance of hat-wearing by a majority of us who spend most of our waking hours indoors protected from the sun by shelter, while those who spend a great deal of time outdoors as part of their jobs continue to wear hats while outside.
Could it be that hats were not worn indoors because there is no direct sunlight, and thus no need?
GENTLE READER: Sun, rain, garbage, oncoming traffic, oncoming athletes, bad hair, no hair -- Miss Manners can think of all kinds of reasons to wear head-coverings, quite aside from the main one, which is that they can look rather fetching.
But in many, perhaps most, cultures (Miss Manners hasn't counted), head-coverings are highly symbolic. The specific rules as to who is entitled to wear what, and when and where hats are required to be worn or to be removed, differ among nationalities and religions, and between the genders.
Symbols being, by definition, arbitrary, they do not meet your standard of making practical sense. But one should not underestimate their emotional power. Failing to cover or uncover the head, according to the particular customs, can be interpreted as a deliberate sign of disrespect, with resulting consequences.
And yes, that applies to baseball caps.