What do we call it when someone injures himself on purpose because of a mental problem? For example, they cut their own flesh or something.
The generic terminology is self-harm:
: the act of purposely hurting oneself (as by cutting or burning the skin) as an emotional coping mechanism
• Although most cutters are young women who have been emotionally, sexually, or physically abused as children, [Marilee] Strong's research shows that this specific type of self-harm also appears in other groups. —Maria Simson et al.
As mentioned in a comment, and by Merriam-Webster, this can also be known as self-injury or self-mutilation.
If you're referring to any type of injury, then it can be called self-injury. If you're referring specifically to people who "cut their own flesh", then that's called cutting. Both of these words have verb forms.
This short passage from TeensHealth shows both the noun and verb forms of these words in action (emphasis added):
Injuring yourself on purpose by making scratches or cuts on your body with a sharp object — enough to break the skin and make it bleed — is called cutting. Cutting is a type of self-injury, or SI. People who cut often start cutting in their young teens. Some continue to cut into adulthood.
People may cut themselves on their wrists, arms, legs, or bellies. Some people self-injure by burning their skin with the end of a cigarette or lighted match.
We can use self-laceration which means:
the act or action of cutting or tearing one's own flesh