When referring to Matthew 7:21-23, it is best to keep that in context of Matthew 7:13-27.That "friend" is the fake religious Christian who perform miracle, cast out demon and speak prophecies in Jesus's name, but the Lord never knew them (Matt. 7:21-23).
Jesus was announcing an iniquity that false prophets will come in to the church and lead many astray in Matthew 7:13-16. I can apply that to the modified Nicene creed of 381 A.D. where they introduced the practice of worshipping the Holy Ghost with the Father & the Son thus broadening the way in the worship place when Jesus is the only way to come to God the Father by in worship John 14:6 and the Son is the only way to honor the Father by per John 5:22-23.
The obvious apostasy is when believers believe they can receive the Holy Spirit again after a sign of tongues ignoring the warning here in 2 Corinthians 11:1-4 & 2 Corinthians 13:5 or for the first time in seeking that sign at their salvation moment per Matthew 12:39-40 committing spiritual adultery as these did in the church at Thyatira in Revelation 2:18-25 having known the utter depths of Satan for which they speak with tongues for "private use" Isaiah 8:19 & 1 Timothy 4:1.
As if that kind of tongues wasn;t bad enough for a sign, believers continue to seek to receive the Holy Spirit after other sensational signs in the flesh like "slain in the spirit" and "holy laughter" movement. While some of those who use tongues for private use will argue that slain in the spirit and holy laughter is not of God, they forget that those paranormal events occur by seeking to receive the Holy Spirit again or for the first time by seeking a sign thus what Jesus said for our discernment in Matthew 7:17-20 because they all came from the same evil tree of believing they can receive the Holy Spirit by a sign rather than by faith in Jesus Christ Hebrews 11:1-2 & Galatians 3:14 & Galatians 3:26.
Then we come to your Matthew 7:21-23 where by those apostate movements of what they believe is the Holy Spirit, miracles have supposedly occurred as they can say they had prophesied in His name, cast out devils in His name, and done many wonderful works in His name in those movements and others like them like Ernest Angeley's Healing Crusade where he announces the Holy Spirit falling on believers in causing them to fall backwards that brings about miracles..
And then we close with Matthew 7:24-27 as a warning to believers to avoid falling like that in a loss of self control ( those who seek tongues do have some where they fall backwards in a loss of self control to get that tongue. ) about heeding His words by how we can avoid falling like that when that storm i.e. that apostate movement comes & heeding His words is Jesus is the only way to come to God the Father by and not the Holy Spirit nor the Trinity so we can avoid the spirits of the antichrist when it comes in worship whereby many that have the spotlight on the Holy Spirit or the Trinity in worship, will think that was the Holy Spirit that had come, barring the initial visit where there was no invoking Him to come.
So that is why Jesus addressed them as "friend" because He had died for them but naked of their wedding garment hence that faith in Jesus Christ on how He has been received rather than by a sign at the calling of the gospel & no other time.
Like it or not saved believers will go astray & have, but they are still His & thus still saved because any iniquity that is on that foundation that defiles the temple of God, He will deny them Titus 1:16 & be left behind 1 Corinthians 3:10-17 to die but their spirits will be with the Lord in Heaven 2 Corinthians 5:7-11 awaiting for their resurrection after the great tribulation Revelation 20:1-6 to serve Him as vessels unto dishonor in His house, vessels of wood & earth 2 Timothy 2:18-21.