Banks are regulated* - they are not free to just invest in whatever gives the best risk-adjusted reward.
The assets that banks hold are given regulatory “haircuts”. That means, in a simplified fashion, that if a bank lends you $100, it might be allowed to borrow $95 of that money from somebody else, but the other $5 must come from the bank’s equity capital. This stops the bank from expanding its balance sheet indefinitely by an unlimited amount of borrowing and lending.
Of course, a sensible bank would do this anyway. But the problem is, when the economy is strong, there are no defaults, so bankers who take more risk just make more money. There is little market incentive to be prudent. So it has to be done through regulation.
To some degree, the regulatory “haircutting” obviously reflects the risk of the asset. A more risky type of asset attracts a larger haircut, thus giving the bank an incentive to demand a higher rate of return from risky assets, since they tie up more of its equity capital.
However, sometimes the haircuts (or other explicit regulations) are designed to just prevent banks doing too much of certain things. One of those things would be investing in the stock market. That’s because of the correlation between risks, and the importance of banks to the overall economy. If the stock market crashes, everyone is under pressure, people start defaulting on loans, and confidence can crumble, causing a feedback of more defaults. It’s important to ensure that in a market crash, banks are not exposed to the extra risk of losing massive amounts of money on their own stock portfolio. If people can at least be sure that the banks are safe and stable, then it’s much easier for businesses to hunker down in a crisis, and for confidence to gradually rebuild.
*All of this is what should happen in a sensibly run economy. In practice, well…