The Succubus became part of medival mythology in the fourteenth century. A supernatural entity that appears in dreams taking the form of a beautiful woman in order to seduce men, usually through sexual activity. The incubus was the mythical male counterpart.
The church believed repeated sexual activity with a succubus reduced health even leading to death. But what a way to go.
A basis in fact
Many legends are based on actual events and it is postulated the Succubus myth was an underlying explanation for wet dreams or nocturnal emmission.
There are other theories as to the underlying basis for the mythology but I can see how ” A demon came and had sex with me while I was sleeping” would be a good way to explain the stains on the sheets.
In the case of the incubus sadly one potential explanation is based on the rape of sleeping women . The rapist blamed it on the demon incubus.
Multi cultural
Although the succubus/incubus myth was european in orgin similar demons exist in other cultures. For example in the folklore of India the Yaksha and Yakashini are similar demonic creatures and Arab culture has the qarinah.
Serving a purpose
One purpose of myths like these is to manage behavior. The evils of sex with yourself at night our couched of a scary devil. This is intended to deter the activity.
If that does not work there is always the threat of poor health or death.