2 – It is disliked for a desert Bedouin to lead a townsman in prayer, because ignorance and roughness tend to prevail among those who dwell in the wilderness. The Exalted says: “The Bedouin Arabs are the most steeped in unbelief and hypocrisy and most likely not to know the limits Allah has revealed to His Messenger.” (al-Tawbah 9:97)
3 – It is also disliked for a flagrant sinner to lead a righteous believer, for his religiosity is deficient and he is lax concerning the obligatory and recommended rulings of the prayer.
4 – It is recommended that leadership in prayer be given to the learned residents of settled areas, to those who are upright and pious and who render the prayer its full due so as to perfect it.
5 – Shaykh al-Islam said: “Praying behind a sinner is forbidden by unanimous agreement of the Muslims, yet the prayer offered behind him remains valid. There is no contradiction between prohibiting his appointment as imam and the soundness of the prayer.”
He—may Allah have mercy on him—further stated: “The principle is that whoever’s own prayer is valid may validly lead others, and the prayer of a sinner is, by consensus, valid. al-Bukhārī relates in his History that ʿAbd al-Karīm al-Jazarī said, ‘I met ten of the Prophet’s Companions who prayed behind tyrannical rulers.’ Likewise, in Ṣaḥīḥ al-Bukhārī 694 it is narrated from Abū Hurayrah that the Prophet ﷺ said: ‘Your imams lead you in prayer; if they do well, you and they share the reward, and if they err, the reward is yours while the burden is on them.’ The general texts on congregational prayer, and many authentic narrations, all prove the validity of praying behind sinners.
He also said: “A person may, by unanimous agreement of the four imams and others, perform the five daily prayers, Jumuʿah, and any other congregational prayer behind someone about whom no innovation or open sin is known. It is not a condition for the follower to know his imam’s creed or to test him; rather, he prays behind one whose outward state is sound.”
Scholarly disagreement:
The Ḥanafīs and Shāfiʿīs deem the leadership of a sinner valid, although giving precedence to the god-fearing is preferable.