Where did Mara come from?

There was a similar discussion here, but it didn’t discuss the ‘purpose’ or use of a Mara character in the suttas.

https://discourse.suttacentral.net/t/dhp-44-are-yama-mara-the-same-or-different/5631