Hadith Q207

إِذِ الذِّكْرُ عَلَى طَهَارَةٍ أَفْضَلُ، لَا أَنَّهُ غَيْرُ جَائِزٍ أَنْ يُذْكَرَ اللَّهُ عَلَى غَيْرِ طُهْرٍ، إِذِ النَّبِيُّ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ قَدْ كَانَ يَذْكُرُ اللَّهَ عَلَى كُلِّ أَحْيَانِهِ‏.‏
It was because mentioning Allah Almighty with purity is more virtuous, not because mentioning Allah Almighty without purity is not permissible. Because the Prophet Muhammad (peace be upon him) used to mention Allah Almighty at all times.
Hadith Reference صحيح ابن خزيمه / جماع أبواب فضول التطهير والاستحباب من غير إيجاب / Q207