Yawning always seemed, to me, to go hand in hand with stretching. I believe the reason we yawn is one in the same with the reason we stretch. By yawning, we are effectively stretching one of the few internal organs under our direct control: the lungs. It stretches the muscles involved in breathing, releasing trapped lactic acid built up over a long day of automated breathing. It improves blood oxygen concentration, which improves wakefulness and attention, which is exactly what you need when you are fatigued. Deep breathing also reduces anxiety, which is why breathwork is heavily incorporated in meditation.
I think the contagious aspect could be an evolutionary behavior. Social mammals tend to work in groups, and “contagious” yawning was likely naturally selected for since all members of an exhausted group who yawn would be more awake and alert in the event of an attack or when hunting prey. These are physically strenuous (and tiring) activities that directly affect the survival of the organism.
It's not that I don't still get tired, of course I do. More that I'm less affected by the ambient heat.
The body do cool down when it sleeps, and many people do feel aided in falling asleep if they lower their body temperature a bit. Yawning might just be that, a small trick to get the body to do a short temporary jump towards a more sleepy state, and a social ques with a minor biological aspect to trigger similar state in a social group. It could also be the other way around, that being a social trigger with a minor biological aspect attached that happens to have a real use in other animals.
https://www.sleep.org/does-your-body-temperature-change-whil...
Maybe yawning helps you get to the right temperature.
When we are distressed, we release cortisol, which is a pro-oxidizing free radical that raises body temperature and blood pressure. Signing involves a fast and prolonged exhalation of air. We tend to sigh when we are distressed, so it possibly serves as a way to cool ourselves down similar to an exhaust fan on a desktop chassis.
It might also be a type of communication which goes against the typical 'pack' animal communication hierarchy. It might make sense for the pack to sleep when any of the pack is tired, rather than waiting for the alpha-dog to feel tired. 'contagious' yawning allows that.