Yes, it is legal to deposit a cheque that says pay to the order of Cash. It is also legal to deposit a cheque that is payable to you. In neither case are you guaranteed that you actually have the money the cheque claims to provide you.
The cheque may have been stopped by the person who wrote it, either because they lost it or because they are deliberately trying to scam someone. The cheque may be forged. Or it may just have been written on an account that doesn't have enough money in it to cover the cheque. When any of these things (and many more possibilities) happen, the cheque fails to clear and the money that had been put into your account is removed. This is legal and how things work with banks. Ordinary cheques are not money, they are just the promise of money, and sometimes, that promise isn't kept.
This removal doesn't normally mean that they close your account, but it's possible the bank was concerned that your behaviour in this wasn't entirely up to their standards, so they would rather not have your business. This is also legal. You can ask the bank to explain further if you feel that you did nothing wrong, but I expect it would be quicker and easier to take your business to a different bank. And in the future, if someone wants to pay you by cheque, remember that it's only a promise of money. Many businesses no longer take cheques, and with good reason.