Bathsheba’s son by David was Solomon, thus the genealogy of Jesus Christ from David through her. God has his own purposes.
IMO that reason may be to show everyone is flawed but HE can still do great things thru people willing to repent and let HIM LEAD.