Of course it is legal - why shouldn't it be?
Mind you - you have to tell whoever gives you the mortgage that it's a "Buy to Let" mortgage - but a lot of mortgage providers offer them e.g.
You are supposed to inform your bank when you do this - but many do not because banks will increase interest as you are effectively converting to a buy-to-let.
I don't know anyone that has informed their bank and non-enforcement of this rule is allowing people with 5-10% deposits obtain buy-to-let mortgages when normally they would require 25%.
Oh and it's not legal. It's a breach of contract if you do not inform your bank.
Not legal as in against the law, or just subject to whatever clauses are there in the contract to cover that particular outcome?
A common "business" in the US is to use various "housing assistance" programs to leverage cash for non-owner occupied housing. These debts are dischargeable in bankruptcy. Generally there are limits to about $2M aggregate and/or 7 properties, depending upon how you work the system. A great many of these "investors" are real estate brokers.
Imagine if your government announced software developers could get 3-10% cash down leverage on up to 6-7 $300K simultaneous projects. If you fail, the taxpayer picks up the tab. And you don't even have to go through YC to get it. Just show that someone else sold their similar project for however much you are proposing you get funded for.
One way to reform this system is to simply turn non-owner-occupied housing into non-dischargeable debt, with a 7-year phase-in on all legacy debt, and disgorging such mortgages back into the private market.
I'm very hard-pressed to come up with any other business where 4-5 figure sums of USD cash can lock in 6-7 figure values of assets, at preferential interest rates, over very long payment terms, with only consumer-grade credit ranking and formulaic "comparable value" asset analysis for loan approval. If you've ever owned a business for any length of time sufficient to talk with your business banker about credit terms, you'll immediately recognize the extreme disparity between those terms and normal business loans and lines of credit terms. It is indefensible to pledge taxpayer financial support to real estate businesses via these loan vehicles.