Legally anyone with a medical license, and meeting their locality's requirements can practice any medicine, just that only a fool would do something like be a psychiatrist and practice surgery.
Now all that said and done not being in the specialty, while not illegal, opens up the problems of if being sued or otherwise investigated the question will come up as to why the physician is practicing outside their usual scope of practice and it could get the person in trouble. Also insurance, as mentioned above, might not want to reimburse for a doc who's not practicing within the usual scope of practice. If the doc isn't going to get paid, that in and of itself will prevent the overwhelming majority from wanting to do it.
Years ago I remember the first Ketamine clinics for depression being operated by an anesthesiologist. While they're not psychiatrists, anesthesiology was the field that had the most experience at that time with that medication. Highly questionable, but these clinics did it.
As an aside, so much of what we do is highly controlled by reimbursement. E.g. if Medicare starts reimbursing something that wasn't the norm at that time it often times then becomes a new norm cause there's not reimbursement and people could argue that an established government entity reimburses for it from a legal vantage.