In the Christian tradition, men do not wear hats inside a church because of St. Paul’s instructions to the Corinthians:
Any man who prays or prophesies with his head covered dishonors his head… For a man ought not to cover his head, since he is the image and glory of God
(1 Cor. 11:4, 7).
In Western society, men do not wear hats in most buildings, particularly in the home, as a sign of respect; they also do not wear them in the presence of women, particularly older women, as a sign of deference.
I suggest though that you disabuse your son of the idea that every rule of etiquette requires a logical reason. Many rules of etiquette are in place simply because that is the code of behavior a particular society agreed upon as appropriate. Following social etiquette, even when it doesn’t have a logical reason but merely is “the way we do things,” is a necessary ingredient to social harmony.