Sometimes it hurts when I have sex. Why might this be?

There are a few reasons this might be the case. It could be medical, psychological or sexual. A medical issue might need to be address by a doctor and a psychological a mental health professional, but first try these sexual tips to see if the pain subsides.
I know you have probably heard it before, but many couples don’t engage in enough foreplay. Foreplay helps a woman’s body to prepared for penetration. It helps her to become lubricated and for something called tilting. Biologically our bodies prepare to be entered by our uterus slightly tiling to make more space. The lack of space due to a lack of foreplay could be the cause of the pain or discomfort.
The other issues could be a lack of lubrication and even though you might engage in foreplay, rougher sex or longer lasting sex might allow for frication to occur and for you to experience pain. Make sure you apply lubrication before pain occurs as by the time any pain it present it could be too late and tears might occurring due to rubbing and friction.
If this does solve the problem, make sure you consult with a health care professional.