As said ,it's not illegal. The mortgage might contain a term prohibiting it or might have one saying that the lender's consent is required or it might not. Any lender's principal concerns are that the mortgage be paid and that the property, which is the security, is not adversely affected as to value. A letting might go some way to ensuring that the mortgage is being paid ! Tenants who might damage the structure or otherwise affect the value of the security, are another matter, of course.
A buy-to-let mortgage would be a rare thing now.Before these existed, landlords often had 'ordinary' mortgages and used to let their properties without fuss.