The reason for yawning is to signal to others that you're ready to sleep and when there are enough people yawning the assumption is everyone is safe and yeah we could just sleep now and save some energy. That's why it's "contagious". It's a quick way of polling the group; not also yawning means you feel unsafe enough to override your yawn reflex.
Source: I made it up
the reason for yawn is to signal ooo big stretch incoming