Within the context of protecting consumers, why wouldn't we want to allow judges to rewrite the terms of a mortgage?For example, if the bank's investors are consumers.
As you say, one reason for this would be to protect the bank's investors from taking losses. Many of the investors could well be consumers since, for example, pension funds often are big investors in various enterprises). If the banks lose value, individual investors could be hurt.
Another reason why we might want to prevent judges from doing this is to protect all the people who have followed the rules (and will do so in the future) from paying for the mistakes of others. If banks are forced by a judge to take less money than they are due, they may be hurt financially. In order to recoup their losses, they may have to charge higher interest rates to future buyers. They may also be less likely to lend for fear of being ordered to take "haircuts" by judges. This would mean that having judges help people who are having trouble with their mortgages might hurt other people who want mortgages in the future.