A man who had a hammer on him in a hospital emergency department because he was “afraid of being attacked by Muslims” has appeared in court.