Gauge invariant Barr-Zee type contributions to fermionic EDMs in the two-Higgs doublet models