Doctors, lawyers, teachers, and professional athletes all experience social reward and/or social contexts to their work. Software engineering is interfacing with an unthinking, unfeeling machine for many hours a day.
Furthermore, the whole point of the vast majority of software engineering is to sell loot boxes to kids to increase shareholder wealth for a pittance of the wealth created (or something not too different from that for at least 60% of companies). How many mainstream software companies are contributing to building a society that you yourself would like to live in?
To put it bluntly, many bad parents abandoned their kids to the internet where they found solace. Software engineering is a natural extension of spending too much time using the internet and becoming curious how it works.
Poor mental health is a function of unmet needs... not feeling attractive, not feeling like you are contributing to the world or society at large, not getting laid, not feeling loved, not feeling worthy of love. All of these represent unmet needs, and I think software engineers struggle with these things more than a significant number of other professions because they are frequently intrinsic to why people become software engineers in the first place, machines don't care how socially inept you are, patients, clients, and judges do.